//
//  AppDelegate.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/9.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "AppDelegate.h"
#import "LFBTabBarController.h"
#import "LFBStartController.h"
#import "LFBShoppingCartManager.h"

@interface AppDelegate ()

@property(nonatomic,strong) UITabBarItem *shoppingTabBarItem;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //判断是否是真机
    [self judgeSystem];
    
    [self setupNotification];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    LFBTabBarController *controller = [[LFBTabBarController alloc] init];
    
//    LFBStartController *startVC = [LFBStartController new];
    
    self.window.rootViewController = controller;
//    self.window.rootViewController = startVC;
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    //设置tabbar中item的begValue
    LFBTabBarController *tabBarController = (LFBTabBarController *)self.window.rootViewController;
    UIViewController *shoppingVC = [tabBarController.viewControllers objectAtIndex:2];
    UITabBarItem *shoppingTabBarItem = shoppingVC.tabBarItem;
    self.shoppingTabBarItem = shoppingTabBarItem;
//    tabBarItem.badgeValue = @"1";
    
    return YES;
}

- (void)teardownNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shoppingCartAnimationNotification:) name:LFBShoppingCartAnimationNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shoppingCartAnimationNotification:) name:LFBDetailProductInfoViewNumChangeNotification object:nil];
}

- (void)shoppingCartAnimationNotification:(NSNotification *)noti
{
    NSInteger sumNums = [LFBShoppingCartManager sharedManager].sumNums;
    
    NSString *sumNumStr = @(sumNums).description;
    
    if(sumNums == 0)
    {
        sumNumStr = @"";
        self.shoppingTabBarItem.badgeColor = [UIColor clearColor];
    }
    else
    {
        self.shoppingTabBarItem.badgeColor = [UIColor redColor];
    }
    
    self.shoppingTabBarItem.badgeValue = sumNumStr;
    
}

- (void)startButtonActionNotification:(NSNotification *)noti
{
    LFBTabBarController *controller = [[LFBTabBarController alloc] init];
    self.window.rootViewController = controller;
}

- (void)dealloc
{
    [self teardownNotification];
}

- (void)judgeSystem
{
    if(TARGET_IPHONE_SIMULATOR)
    {
        NSLog(@"~~模拟器");
    }else if(TARGET_OS_IPHONE)
    {
        NSLog(@"~~手机");
    }
}

@end
